#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>

mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg) {
    current_state = *msg;
}

int main(int argc, char** argv) {
    ros::init(argc, argv, "offb_TI_AI");
    ros::NodeHandle nh;

    ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>("iris_0/mavros/state", 10, state_cb);
    ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>("iris_0/mavros/setpoint_position/local", 10);
    ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>("iris_0/mavros/cmd/arming");
    ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>("iris_0/mavros/set_mode");

    ros::Rate rate(20.0);

    // 等待连接
    while (ros::ok() && !current_state.connected) {
        ros::spinOnce();
        rate.sleep();
    }

    geometry_msgs::PoseStamped pose;
    pose.pose.position.x = 0;
    pose.pose.position.y = 0;
    pose.pose.position.z = 1.2;

    // 发送100个预设点以进入OFFBOARD模式
    for (int i = 0; ros::ok() && i < 100; ++i) {
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }

    // 设置模式/解锁请求
    mavros_msgs::SetMode offb_set_mode;
    offb_set_mode.request.custom_mode = "OFFBOARD";

    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;

    // 切换OFFBOARD模式
    ROS_INFO("Waiting for OFFBOARD mode...");
    while (ros::ok() && current_state.mode != "OFFBOARD") {
        set_mode_client.call(offb_set_mode);
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }
    ROS_INFO("Offboard mode enabled");

    // 解锁
    ROS_INFO("Waiting for vehicle armed...");
    while (ros::ok() && !current_state.armed) {
        arming_client.call(arm_cmd);
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }
    ROS_INFO("Vehicle armed");

    // 起飞后悬停5秒
    ros::Time hover_start = ros::Time::now();
    while (ros::ok() && (ros::Time::now() - hover_start < ros::Duration(5.0))) {
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }
    ROS_INFO("Takeoff complete, hovering done. Starting Afghan-style descent...");

    // 斜角下降
    double step = 0.01;
    while (ros::ok() && pose.pose.position.z > 0.2) {
        pose.pose.position.x += step;
        pose.pose.position.z -= step;
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }

    // 切换至自动降落
    ros::Time last_request = ros::Time::now();
    mavros_msgs::SetMode land_set_mode;
    land_set_mode.request.custom_mode = "AUTO.LAND";
    
    ROS_INFO("Switching to AUTO.LAND...");
    while (ros::ok() && current_state.mode != "AUTO.LAND") {
        if (set_mode_client.call(land_set_mode) && land_set_mode.response.mode_sent) {
            ROS_INFO("AUTO.LAND enabled");
        }
        local_pos_pub.publish(pose);  // 维持位置
        ros::spinOnce();
        rate.sleep();
    }

    // 等待自动锁桨
    ROS_INFO("Waiting for disarm...");
    while (ros::ok() && current_state.armed) {
        ros::spinOnce();
        rate.sleep();
    }

    ROS_INFO("Landing complete, vehicle disarmed.");
    return 0;
}
